package com.nageoffer.jobtrain.admin.service.filter;

import com.nageoffer.jobtrain.admin.common.enums.OjQuestionTypeEnum;
import com.nageoffer.jobtrain.admin.dto.req.OjQuestionAddReqDTO;
import com.nageoffer.jobtrain.admin.service.chain.OjQuestionAbstractChainHandler;
import com.nageoffer.jobtrain.common.web.exception.ClientException;
import org.springframework.stereotype.Component;

import java.util.Arrays;

import static com.nageoffer.jobtrain.admin.common.enums.ChainBizMarkEnum.OJ_QUESTION_CREATE_MARK;

/**
 * author：唐晨
 * Oj题目创建基础参数校验责任链实现
 */
@Component
public class OjQuestionCreateParamBaseVerifyChainFilter implements OjQuestionAbstractChainHandler<OjQuestionAddReqDTO> {
    private final Integer totalTimeLimit = 1000;  // 单位是毫秒
    private final Integer totalSpaceLimit = 256 * 1024 * 1024; // 单位是字节

    @Override
    public void handler(OjQuestionAddReqDTO requestParam) {
        // 进行题目难度的匹配
        boolean difficultyAnyMatch = Arrays.stream(OjQuestionTypeEnum.values())
                .anyMatch(enumConstant -> enumConstant.getType() == requestParam.getDifficulty());
        if (!difficultyAnyMatch) {
            throw new ClientException("Oj题目难度值不存在");
        }

        // 时间限制
        if (requestParam.getTimeLimit() > totalTimeLimit) {
            throw new ClientException("Oj题目时间限制设置有误");
        }

        // 空间限制
        if (requestParam.getSpaceLimit() > totalSpaceLimit) {
            throw new ClientException("Oj题目空间限制设置有误");
        }

        // 后续增加更多限制......
    }

    @Override
    public String mark() {
        return OJ_QUESTION_CREATE_MARK.name();
    }

    @Override
    public int getOrder() {
        return 10;
    }
}
